﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;

namespace Charities
{
    public partial class Supplies : Form
    {
        int userId = -1;
        Form fo = null;
        public bool is_loaded = false;
        public Supplies(int id,Form m)
        {
            fo = m;
            this.MdiParent = m;
            userId = id;
            InitializeComponent();
        }
        ModelContainer ctx = new ModelContainer();
        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void Supplies_Load(object sender, EventArgs e)
        {
            is_loaded = true;
            // add this form to window list
            User user = ctx.Users.Single(u => u.Id == userId);
            foreach (var Item in user.Role.Permissions)
            {
                switch (Item.Name)
                {
                    case "SUPPLIES_SUPPLY_CART":
                        btn_new_Supply.Visible = true;
                        break;
                    case "SUPPTIES_ENTRIES_REPORT":
                        btn_Entryies_Report.Visible = true;
                        break;
                    default:
                        break;
                }
            }

            ((Forms.FormMDI)fo).addFormToWindowsList(this);
            //ImportSupplies();
            supplyBindingSource.DataSource = (from s in ctx.Supplies select new { Name = s.Name, Id = s.Id }).ToList();
        }

        private void dgv_AddMaterials_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            int id;
            try
            {
                if (dgv_AddMaterials.Rows[e.RowIndex].Cells[0].Value != null)
                {
                    id = (int)dgv_AddMaterials.Rows[e.RowIndex].Cells[0].Value;
                    //  string name = dgv_AddMaterials.Rows[e.RowIndex].Cells[1].Value.ToString();
                    Supply sup = ctx.Supplies.Single(s => s.Id == id);
                    Batch b = new Batch();
                    b.SupplyId = id;
                    b.Amount = double.Parse(dgv_AddMaterials.Rows[e.RowIndex].Cells[1].Value.ToString());
                    b.Entry = new Entry { DateOfEntry = dtp_Date.Value, Notes = txb_Notes.Text, From_ = txb_From.Text, InsertDate = DateTime.Now, InsertUId = 1 };
                    b.InsertDate = dtp_Date.Value;
                    b.InsertUId = 1;
                    sup.Amount = sup.Amount + double.Parse(dgv_AddMaterials.Rows[e.RowIndex].Cells[1].Value.ToString());
                    ctx.Batches.Add(b);
                    ctx.SaveChanges();
                }
                ImportSupplies();

            }
            catch 
            {
                MessageBox.Show("يرجى إدخال قيمة صحيحة ");                
            }
         
        }
        private void ImportSupplies()
        {
            dgv_SuppliesInWhereouse.DataSource = null;
            var supplies = (from c in ctx.Supplies
                            from p in ctx.SupplyTypes
                            where c.SupplyTypeId == p.Id
                            select new { c.Id, c.Name, c.Amount, c.UnitOfMeasurement, SupplyTyp = p.Name }).ToList();
            dgv_SuppliesInWhereouse.DataSource = supplies;
            dgv_SuppliesInWhereouse.Columns[0].Visible = false;
            dgv_SuppliesInWhereouse.Columns[0].Name = "Id";
            dgv_SuppliesInWhereouse.Columns[1].HeaderText = "المعونة";
            dgv_SuppliesInWhereouse.Columns[2].HeaderText = "الكمية";
            dgv_SuppliesInWhereouse.Columns[3].HeaderText = "الوحدة";
            dgv_SuppliesInWhereouse.Columns[4].HeaderText = "نوع المعونة";
       
        }
        private void SearchSupplies(string search)
        {
            dgv_SuppliesInWhereouse.DataSource = null;
            var supplies = (from c in ctx.Supplies
                            from p in ctx.SupplyTypes
                            where c.SupplyTypeId == p.Id && (c.Name.Contains(search) || c.SupplyType.Name.Contains(search)||c.UnitOfMeasurement.Contains(search))
                            select new { c.Id, c.Name, c.Amount, c.UnitOfMeasurement, SupplyTyp = p.Name }).ToList();
            dgv_SuppliesInWhereouse.DataSource = supplies;
            dgv_SuppliesInWhereouse.Columns[0].Visible = false;
            dgv_SuppliesInWhereouse.Columns[0].Name = "Id";
            dgv_SuppliesInWhereouse.Columns[1].HeaderText = "المعونة";
            dgv_SuppliesInWhereouse.Columns[2].HeaderText = "الكمية";
            dgv_SuppliesInWhereouse.Columns[3].HeaderText = "الوحدة";
            dgv_SuppliesInWhereouse.Columns[4].HeaderText = "نوع المعونة";

        }
        private void dgv_AddMaterials_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("يرجى إدخال قيمة صحيحة ");       
        }

        private void btn_Entryies_Report_Click(object sender, EventArgs e)
        {
            EntriesReport en = new EntriesReport(userId,fo);
            en.Show();
        }

        private void btn_Dispose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_new_Supply_Click(object sender, EventArgs e)
        {
            Forms.NewSupply n = new Forms.NewSupply(userId, fo);
            n.Show();
        }

        private void Supplies_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void Supplies_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
            ImportSupplies();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            SearchSupplies(txb_search.Text);
        }

        private void txb_search_MouseHover(object sender, EventArgs e)
        {
            toolTipsearch.Show(" اسماء المواد أو نوع المعونة",this,100);
        }

        private void label5_MouseHover(object sender, EventArgs e)
        {
            toolTipsearch.Show(" اسماء المواد أو نوع المعونة", this);
        }

        private void dgv_SuppliesInWhereouse_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_SuppliesInWhereouse.Rows[e.RowIndex].Cells["Id"].Value != null)
            {
                int id = (int)dgv_SuppliesInWhereouse.Rows[e.RowIndex].Cells[0].Value;
                Forms.NewSupply ne = new Forms.NewSupply(userId, fo, id);
                ne.Show();
            }
        }

      
    }
}
